package set;

import java.util.Comparator;
import java.util.TreeSet;
import set.Student;
public class TreeSetDemo2 {
    public static void main(String[] args) {
//        如果同时具备比较器和自然排序，会优先按照比较强的规则进行排序
        TreeSet<Student> stu = new TreeSet<>(new Comparator<Student>(){
            @Override
            public int compare(Student o1, Student o2) {
                int ageResult = o1.getAge() - o2.getAge();
                return ageResult == 0 ? o1.getName().compareTo(o2.getName()) : ageResult;
            }
        });
        stu.add(new Student("zhangsan",20));
        stu.add(new Student("lisi",22));
        stu.add(new Student("wangwu",25));
        stu.add(new Student("zhaolv",23));
        System.out.println(stu);
    }
}
